﻿

//钩子函数
$(function () {
    Show();
});
//方法
function Show() {
    //显示
    layui.use(function () {
        var table = layui.table;
        //查询条件
        var warehouseNo = $("#WarehouseNo").val();
        var locationCoding = $("#LocationCoding").val();
        var warehouseState = $("#WarehouseState").val();
        //console.log(locationCoding)
        // 创建渲染实例
        table.render({
            elem: '#warehousetable',
            page: {
                limit: 5,
                limits: [5, 8, 10]
            },
            cols: [[
                { type: 'checkbox', fixed: 'left' },
                { field: 'warehouseNo', width: 110, title: '仓库编号' },
                { field: 'warehouseName', width: 110, title: '仓库名称' },
                { field: 'reservoirArea', width: 110, title: '仓库区域' },
                { field: 'locationCoding', width: 110, title: '位置编码' },
                { field: 'warehouseArea', width: 110, title: '面积' },
                { field: 'warehouseHead', width: 110, title: '负责人' },
                {
                    field: 'warehouseState', width: 110, title: '状态', templet: function (x) {
                        if (x.warehouseState === true) {
                            return '<span>正常</span>'
                        }
                        else {
                            return '<span>异常</span>'
                        }
                    }
                },
                {
                    field: '', width: 110, title: '平面图', templet: function (a) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="lookplanData()">查看</a>'
                    }
                },
                { field: 'warehouseRemark', width: 110, title: '备注' },
                {
                    fixed: '', title: '操作', width: 170, templet: function (d) {
                        return '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick="editData(' + d.warehouseId + ')">编辑</a>'
                            + '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick ="lookData(' + d.warehouseId + ')">查看</a>'
                            + '<a class="layui-btn layui-btn-edit layui-btn-xs" onclick ="deleteData(' + d.warehouseId + ')">删除</a> '
                    }
                }
            ]],
            // 初始时可以先不加载数据，等待 ajax 请求成功后再更新
            data: []
        });

        $.ajax({
            url: '/Warehouse/GetWarehouseShow',
            type: 'get',
            data: {
                pageindex: 1,
                pagesize: 100,
                token: "",
                WarehouseNo: warehouseNo,
                LocationCoding: locationCoding,
                WarehouseState: warehouseState
            },
            success: function (data) {
                data = JSON.parse(data);
                console.log(data.data.list);
                // 在 ajax 请求成功后，使用正确的表格 ID 进行 reload
                table.reload('warehousetable', {
                    page: {
                        limit: 5,
                        limits: [5, 8, 10]
                    },
                    data: data.data.list
                });
            },
            error: function (res, msg) {
                console.log(res, msg)
            }
        })
        // 点击导出按钮
        document.getElementById('ExportExcel').addEventListener('click', function () {
            var checkStatus = table.checkStatus('warehousetable');  // 获取当前表格的选中状态
            var selectedData = checkStatus.data;  // 获取选中的数据

            if (selectedData.length === 0) {
                alert('请先选择要导出的数据!');
                return;
            }

            // 使用 XLSX 导出选中的数据
            var wb = XLSX.utils.book_new();
            var ws = XLSX.utils.json_to_sheet(selectedData); // 将选中的数据转换为工作表
            XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

            // 导出为 Excel 文件
            XLSX.writeFile(wb, '选中的数据.xlsx');
        });

    });
    //查看仓库平面图
    window.lookplanData = function () {
        location.href = '/Warehouse/WarehousePlanShow'
    }
    //查看仓库
    window.lookData = function (warehouseId) {
        localStorage["warehouseId"] = warehouseId;
        location.href = '/Warehouse/WarehouseLook'
    }
    //删除仓库
    window.deleteData = function (warehouseId) {
        layer.confirm("确认要删除吗?", function (index) {
            console.log(warehouseId);
            $.ajax({
                type: 'post',
                url: '../Warehouse/DelWarehouse',
                data: {
                    WarehouseId: warehouseId,
                    token: localStorage.getItem("token")
                },
                success: function (res) {
                    var obj = JSON.parse(res);
                    if (obj.code === 0) {
                        layer.msg("删除成功");
                        Show();//重新加载显示
                    }
                    else {
                        layer.msg("删除失败");
                    }
                },
                error: function (err) {
                    layer.msg('删除失败，请稍后再试', {
                        time: 2000,
                        icon: 2
                    })
                }
            })
            layer.close(index);
        })
    }
    //编辑仓库
    window.editData = function (warehouseId) {
        localStorage["warehouseId"] = warehouseId;
        location.href = '/Warehouse/WarehouseUpd'
    }
}